// Copyright 2010 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Provides a class to write out creatives to a spreadsheet.
 */

goog.provide('ads_mult.SpreadsheetWriter');

goog.require('ads_mult.common');


/**
 * Class to write Processed data to spreadsheet.
 * @param {Spreadsheet} spreadsheet Spreadsheet object.
 * @constructor
 */
ads_mult.SpreadsheetWriter = function(spreadsheet) {
  this.spreadsheet = spreadsheet;
};


/**
 * Returns today's current date
 * @param {string} opt_delim Separator for date fields.  If undefined,
 *     use '/'.
 * @return {string} Today's date, in mm/dd/yyyy format.
 * @private
 */
ads_mult.SpreadsheetWriter.getToday_ = function(opt_delim) {
  var delim = opt_delim || '/';
  var dt = new Date();
  return (dt.getMonth() + 1) + delim + dt.getDate() + delim + dt.getFullYear();
};


/**
 * Writes output data to sheet.
 * @param {string} sheetName Name for new sheet to write to.
 * @param {Array.<string>} headers An Array of strings that should be the column
 *     names.
 * @param {Array.<Object.<string,string>>} outputs An Array of objects.
 *     outputs[0][0] would contain the data that goes in the cell A1.
 * @param {Object} opt_limits Character limits for each column.
 *     If 'title' must be no more than 1024, opt_limits['title'] should be 1024.
 */
ads_mult.SpreadsheetWriter.prototype.writeOutputs = function(
    sheetName, headers, outputs, opt_limits) {
  var normHeaders = ads_mult.common.normalizeHeaders(headers);
  var campaignName = outputs[0][normHeaders[0]];
  sheetName += '-' + campaignName + '-' + ads_mult.SpreadsheetWriter.getToday_();
  var sheet = this.spreadsheet.insertSheet(
      sheetName,
      this.spreadsheet.getNumSheets());

  var headersRange = sheet.getRange(1, 1, 1, headers.length);
  headersRange.setValues([headers]);
  // freeze headers row
  sheet.setFrozenRows(1);

  var data = [];
  // for each object in outputs, create row
  for (var i = 0, obj; obj = outputs[i]; i++) {
    var values = [];
    // TODO: Closure? look into goog.array.forEach()
    for (var j = 0, hdr; hdr = normHeaders[j]; j++) {
      values[j] = hdr.length > 0 && obj[hdr] ? obj[hdr] : '';
      // color the cell red if values exceed character limits
      // or if the cell is empty (although it shouldnt be empty)
      if (opt_limits[hdr] &&
          values[j].length > opt_limits[hdr] ||
          ads_mult.common.isCellEmpty(values[j])) {
        var redRange = sheet.getRange(i + 2, j + 1);
        redRange.setBackgroundColor('#F95A5A');
      }
    }
    data[i] = values;
  }

  var destinationRange = sheet.getRange(2, 1, outputs.length, headers.length);
  destinationRange.setValues(data);
};
